﻿#ifndef __SS_TOOLS_H__
#define __SS_TOOLS_H__
#include "../ssBaseDefine.h"

namespace StarSeeker
{

//折半查找,数组必须从小到大排列,对象需要重载"<"和"=="
template<class T>
inline T* HalfSearch(T* arr, u32 count, T &key)
{
	u32 beg=1, end=count;
	u32 divide = (end-beg)>>1;
	while (beg<=end && end>0 && end<=count)
	{
		if (arr[divide-1]==key)
		{
			return arr+divide-1;
		}
		else if (arr[divide-1]<key)//右边
		{
			beg = divide+1;
		}
		else//左边
		{
			end = divide-1;
		}
		divide = ((end-beg)>>1)+beg;
	}
	return 0;
}

//两个32位整数拼成一个64位整数
inline i64 I32MergeI64(i32 a1, i32 a2)
{
	i64 ret = a2;
	ret = ret << 32 | a1;
	return ret;
}
inline u64 U32MergeU64(u32 a1, u32 a2)
{
	u64 ret = a2;
	ret = ret << 32 | a1;
	return ret;
}
//一个64位整数拆成两个32位整数
inline void I64SplitI32(i64& src, i32& a1, i32& a2)
{
	a1 = (i32)src;
	a2 = (i32)(src >> 32);
}
inline void U64SplitU32(u64& src, u32& a1, u32& a2)
{
	a1 = (u32)src;
	a2 = (u32)(src >> 32);
}


}
#endif
